package com.ljz.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.ljz.entity.AirQualityIndex;
import com.ljz.service.impl.AirQualityIndexServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@RestController
public class AirQualityIndexController {
    @Autowired
    private AirQualityIndexServiceImpl airQualityIndexService;
    @RequestMapping("/getAirQualityIndexAll")
    public Map<String, Object> getAirQualityIndexList(Integer currentPage, Integer pageSize) {
        return airQualityIndexService.getAirQualityIndexList(currentPage,pageSize);
    }
    @RequestMapping("/addAir")
    public String addAir(@RequestBody AirQualityIndex airQualityIndex) {
        System.out.println(airQualityIndex);
        return airQualityIndexService.addAirQualityIndex(airQualityIndex)>0?"true":"false";
    }
    @RequestMapping("/delAir")
    public String delAir(Integer id) {
        return airQualityIndexService.delAir(id)>0?"true":"false";
    }
    @RequestMapping("/getAll")
    public PageInfo<AirQualityIndex> getAll(Integer currentPage, Integer pageSize) {
        Page<AirQualityIndex> page =   airQualityIndexService.getAll(currentPage,pageSize);
        return page.toPageInfo();
    }
    @RequestMapping("/getAllByPage")
    public ModelAndView getAllByPage(Integer currentPage, Integer pageSize) {
        if (currentPage==null){
            currentPage=1;
        }
        if (pageSize==null){
            pageSize = 5;
        }
        ModelAndView mv = new ModelAndView();
        Map<String,Object> map = airQualityIndexService.getAirQualityIndexList(currentPage,pageSize);
        List<AirQualityIndex> list = (List<AirQualityIndex>)map.get("list");
        Integer total = (Integer)map.get("total");
        mv.addObject("list",list);
        mv.addObject("total",total);
        mv.addObject("currentPage",currentPage);
        mv.addObject("pageSize",pageSize);
        mv.setViewName("index");
        return mv;
    }
}
